﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fashion.Models;
using System.Data;
using Fashion.ViewModels;

namespace Fashion.Controllers
{
    public class MembershipController : ThemeController
    {
        FashionEntities1 fashionDB = new FashionEntities1();
        //
        // GET: /Membership/

        public ActionResult Index()
        {
            if (Session["userLogged"] != null)
                return RedirectToAction("MyAccount", (User)Session["userLogged"]);
            else
                Session["userLogged"] = new UserViewModel();

            return View();
        }

         //
        // POST: /Membership/Register

        [HttpPost]
        public ActionResult Index(FormCollection formValues)
        {
            try
            {
                    string username = formValues["ctl00$MainContent$loginUserName"].ToString();
                    string password = formValues["ctl00$MainContent$loginPassword"].ToString();

                    if (username != "" && password != "")
                    {
                        var user = fashionDB.Users.Single(u => u.Username == username && u.Password == password);

                        if (user != null)
                        {
                            Session["userLogged"] = user;
                            return RedirectToAction("MyAccount", user);
                        }
                    }
                    return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Membership/Register

        public ActionResult Register()
        {
            return View();
        } 

        //
        // POST: /Membership/Register

        [HttpPost]
        public ActionResult Register(FormCollection formValues)
        {
            User newUser = new User();
            try
            {
                // TODO: Add insert logic here
                newUser.Username = formValues["ctl00$MainContent$regUserName"].ToString();
                if (formValues["ctl00$MainContent$regPassword1"].ToString().CompareTo(formValues["ctl00$MainContent$regPassword2"]) != 0)
                    return View();
                newUser.Password = formValues["ctl00$MainContent$regPassword1"].ToString();

                newUser.Email = formValues["ctl00$MainContent$regEmail"].ToString();
                newUser.FirstName = formValues["ctl00$MainContent$firstName"].ToString();
                newUser.LastName = formValues["ctl00$MainContent$lastName"].ToString();
                newUser.AddressLine1 = formValues["ctl00$MainContent$address1"].ToString();
                newUser.AddressLine2 = formValues["ctl00$MainContent$address2"].ToString();
                newUser.PostalCode = formValues["ctl00$MainContent$postal"].ToString();
                newUser.City = formValues["ctl00$MainContent$city"].ToString();
                newUser.Country = formValues["ctl00$MainContent$countryList"].ToString();
                newUser.Phone = formValues["ctl00$MainContent$phone"].ToString();
                newUser.RoleID = 3; // Customer user
                newUser.Points = 0;
                newUser.RegisterDate = DateTime.Today;

                try
                {
                    fashionDB.AddToUsers(newUser);
                    fashionDB.SaveChanges();

                    return RedirectToAction("MyAccount", newUser);
                }
                catch (Exception)
                {
                    //error display here
                }

                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Membership/ViewProfile
 
        public ActionResult ViewProfile()
        {
            if (Session["userLogged"] == null)
                return View("Index");
            else
            {
                var user = (User)Session["userLogged"];

                return View(user);
            }
        }

        [HttpPost]
        public ActionResult ViewProfile(User currentUser)
        {
            if (Session["userLogged"] == null)
                return View("Index");
            else
            {
                var user = fashionDB.Users.Single(u => u.UserID == currentUser.UserID);
                try
                {
                    if (!string.IsNullOrWhiteSpace(currentUser.FirstName))
                        user.FirstName = currentUser.FirstName;
                    if (!string.IsNullOrWhiteSpace(currentUser.LastName))
                        user.LastName = currentUser.LastName;
                    if (!string.IsNullOrWhiteSpace(currentUser.Email))
                        user.Email = currentUser.Email;
                    if (!string.IsNullOrWhiteSpace(currentUser.AddressLine1))
                        user.AddressLine1 = currentUser.AddressLine1;
                    if (!string.IsNullOrWhiteSpace(currentUser.AddressLine2))
                        user.AddressLine2 = currentUser.AddressLine2;
                    if (!string.IsNullOrWhiteSpace(currentUser.City))
                        user.City = currentUser.City;
                    if (!string.IsNullOrEmpty(currentUser.PostalCode))
                        user.PostalCode = currentUser.PostalCode;
                    if (!string.IsNullOrWhiteSpace(currentUser.Phone))
                        user.Phone = currentUser.Phone;
                    if (!string.IsNullOrWhiteSpace(currentUser.Avatar))
                        user.Avatar = currentUser.Avatar;

                    fashionDB.SaveChanges();
                    Session["userLogged"] = (User)user;
                }
                catch
                {
                    //error here
                }

                return View(currentUser);
            }
        }

        public ActionResult ViewOrder()
        {
            if (Session["userLogged"] != null)
            {
                User currentUser = (User)Session["userLogged"];
                var orderModel = from orders in fashionDB.Orders
                                 where orders.UserID == currentUser.UserID
                                 select orders;

                return View(orderModel);
            }

            return View("Index");
        }

        public ActionResult CancelOrder(int orderID)
        {
            if (Session["userLogged"] != null)
            {
                var order = fashionDB.Orders.Single(o => o.OrderID == orderID);
                string status = order.CurrentStatus.ToString();

                if (string.Compare(status, "pending",true) == 0)
                {
                    order.CurrentStatus = "Canceled";
                    fashionDB.SaveChanges();

                    var orderModel = from orders in fashionDB.Orders
                                     where orders.UserID == order.UserID
                                     select orders;
                    return View("ViewOrder", orderModel);
                }
            }
            return View("Index");
        }

        public ActionResult OrderDetail(int orderID)
        {
            if (Session["userLogged"] != null)
            {
                OrderDetailModel orderModel = new OrderDetailModel(orderID);

                return View(orderModel);
            }
            return View("Index");
        }

        public ActionResult ViewCoupon()
        {
            if (Session["userLogged"] != null)
            {
                User currentUser = (User)Session["userLogged"];
                var couponModel = from coupons in fashionDB.UserCoupons
                                  where coupons.UserID == currentUser.UserID
                                  select coupons;

                foreach (var item in couponModel)
                {
                    var coupon = fashionDB.Coupons.Single(c => c.CouponID == item.CouponID);
                    item.startDate = coupon.StartDate;
                    item.expiredDate = coupon.ExpiredDate;
                    item.description = coupon.Description;
                }

                return View(couponModel);
            }
            return View("Index");
        }

        //
        // GET: /Membership/MyAccount

        public ActionResult MyAccount(User user)
        {
            if (Session["userLogged"] == null)
                return RedirectToAction("Index");

            var viewModel = new UserViewModel(user);

            return View(viewModel);
        }
    }
}
